package com.lock.readwrite;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @PackageName:com.lock.readwrite
 * @ClassName:Test03
 * @Description:  读写互斥
 * @Author: zhangxiaoyu
 * @date 2021/3/8 15:21
 */
public class Test03 {

    static class Service{
        //先定义读写锁
        ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
        //定义方法读取数据
        public void read(){
            try {
                readWriteLock.readLock().lock();  //申请获得写锁
                System.out.println(Thread.currentThread().getName()+"获得读锁，开始读取数据的时间---"+System.currentTimeMillis());
                Thread.sleep(3000);  //模拟读取数据的用时
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println(Thread.currentThread().getName()+"读取数据完毕时的时间==="+System.currentTimeMillis());
                readWriteLock.readLock().unlock();  //释放读锁
            }
        }

        //定义方法修改数据
        public void write(){
            try {
                readWriteLock.writeLock().lock();  //申请获得写锁
                System.out.println(Thread.currentThread().getName()+"获得写锁，开始修改数据的时间---"+System.currentTimeMillis());
                Thread.sleep(3000);  //模拟修改数据的用时
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println(Thread.currentThread().getName()+"修改数据完毕时的时间==="+System.currentTimeMillis());
                readWriteLock.writeLock().unlock();  //释放写锁
            }
        }


    }

    public static void main(String[] args){

        Service service = new Service();

        //定义一个线程读数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                service.read();
            }
        }).start();

        //定义一个线程写数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                service.write();
            }
        }).start();
    }
}
